//
//  THMyUserInfoViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/1.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMyUserInfoViewController.h"
#import "THIconChangedViewController.h"
@interface THMyUserInfoViewController ()
@property(nonatomic, strong) NSMutableArray<NSArray*>                     *dataSouce;/**<  */
@property(nonatomic, strong) THUserModel                     *userModel;/**<  */
@property(nonatomic, strong) STButton                     *iconButton;/**<  */
@property(nonatomic, strong) STButton                     *bgButton;/**<  */


@end

@implementation THMyUserInfoViewController


#pragma mark --vc生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    self.userModel = STTools_UserManger.defult.stoneobs_fun_get_loginedUser;
    [self config_dataSouce];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [THUserDataController stoneobs_fun_send_get_user_ifno_request_id:@"" handle:^(THUserModel * _Nonnull user) {
//        self.userModel = user;
//        [STTools_UserManger.defult stoneobs_fun_updateUserModel:user];
//        [self config_dataSouce];
//        [self.iconButton sd_setImageWithURL:self.userModel.header_img.mj_url forState:UIControlStateNormal];
//    }];
    [self.iconButton sd_setImageWithURL:self.userModel.header_img.mj_url forState:UIControlStateNormal];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)config_dataSouce{
    self.dataSouce = NSMutableArray.new;
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(20, 0, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:PLACE_HOLDER_IMAGE_1
                                                                  image:nil];
        self.iconButton = stoneobs_ui_button;
        [self.iconButton sd_setImageWithURL:self.userModel.header_img.mj_url forState:UIControlStateNormal];
        stoneobs_ui_button.centerY = 28;
        stoneobs_ui_button.right = UISCREEN_WIDTH - 40;
    });

    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(20, 0, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:PLACE_HOLDER_IMAGE_1
                                                                  image:nil];
        self.bgButton = stoneobs_ui_button;
        [self.bgButton sd_setImageWithURL:self.userModel.bg.mj_url forState:UIControlStateNormal];
        stoneobs_ui_button.centerY = 28;
        stoneobs_ui_button.right = UISCREEN_WIDTH - 40;
    });
    
    ({
        NSArray * array = @[
            [[STTools_TableEasyModel alloc] initWithTextString:@"头像" detailString:@""],
            [[STTools_TableEasyModel alloc] initWithTextString:@"背景" detailString:@""],
            [[STTools_TableEasyModel alloc] initWithTextString:@"昵称" detailString:self.userModel.name],
            [[STTools_TableEasyModel alloc] initWithTextString:@"性别" detailString:self.userModel.cus_sex_string],
            [[STTools_TableEasyModel alloc] initWithTextString:@"生日" detailString:self.userModel.birthday],
            [[STTools_TableEasyModel alloc] initWithTextString:@"学校" detailString:self.userModel.school],
            [[STTools_TableEasyModel alloc] initWithTextString:@"简介" detailString:self.userModel.brief],
            [[STTools_TableEasyModel alloc] initWithTextString:@"身份" detailString:self.userModel.level_text],
        ];
        [self.dataSouce addObject:array];
    });
    
    
    ({
        
        UISwitch * sex_sw = [self defult_switch_with_tag:10000];
        [sex_sw setOn:self.userModel.is_sex_show];
        UISwitch * birthday_sw = [self defult_switch_with_tag:10001];
        [birthday_sw setOn:self.userModel.is_birthday_show];
        UISwitch * school_sw = [self defult_switch_with_tag:10002];
        [school_sw setOn:self.userModel.is_school_show];
        NSArray * array = @[
            [[STTools_TableEasyModel alloc] initWithTextString:@"性别展示" accessoryView:sex_sw],
            [[STTools_TableEasyModel alloc] initWithTextString:@"生日展示" accessoryView:birthday_sw],
            [[STTools_TableEasyModel alloc] initWithTextString:@"学校展示" accessoryView:school_sw]
        ];
        [self.dataSouce addObject:array];
    });
    
    [self.tableView reloadData];
}
- (UISwitch*)defult_switch_with_tag:(NSInteger)tag{
    UISwitch * switchView = UISwitch.new;
    switchView.tag = tag;
    [switchView addTarget:self action:@selector(switch_view_changed:) forControlEvents:UIControlEventValueChanged];
    return switchView;
}
- (void)switch_view_changed:(UISwitch*)switchView{
    NSString * value = @"0";
    if (switchView.isOn) {
        value = @"1";
    }
    if (switchView.tag == 10000) {
        [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"is_sex_show" value:value handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            
        }];
    }
    if (switchView.tag == 10001) {
        [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"is_birthday_show" value:value handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            
        }];
    }
    if (switchView.tag == 10002) {
        [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"is_school_show" value:value handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            
        }];
    }
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return  58;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:model];
    if (indexPath.row == 0 && indexPath.section == 0) {
        [self.iconButton removeFromSuperview];
        [cell addSubview:self.iconButton];
    }
    if (indexPath.row == 1 && indexPath.section == 0 ) {
        [self.bgButton removeFromSuperview];
        [cell addSubview:self.bgButton];
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString isEqualToString:@"头像"]) {
        THIconChangedViewController * vc = THIconChangedViewController.new;
        [vc setDidChangedIcon:^(NSString * _Nonnull icon) {
            self.userModel.header_img = icon;
            [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"header_img" value:icon handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                
            }];
        }];
        [self pushViewController:vc animated:1];
    }
    if ([model.textString isEqualToString:@"背景"]) {
        [self stoneobs_fun_st_showDefultImagePicker:^(UIImage *image) {
            [TMQNUploadManger.manger sendQiNiuWithImage:image handle:^(NSString * _Nonnull url) {
                if (url.length) {
                    [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"bg" value:url handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                        
                        [self.bgButton setBackgroundImage:image forState:UIControlStateNormal];
                    }];
                }
                
            }];
        }];
        

    }
    if ([model.textString isEqualToString:@"昵称"]) {
        STTools_TableViewDetailController * detail =
        [[STTools_TableViewDetailController alloc] initWithPlaceholder:@"请输入昵称"
                                                           title:model.textString
                                                            text:model.detailString
                                                        complete:^(NSString *text, STTools_TableViewDetailController *detailControllerVC) {
            
            if (text.length && ![text isEqualToString:model.detailString]) {
                model.detailString = text;
                [self.tableView reloadData];
                [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"name" value:text handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                    
                }];
            }
            if (!text.length) {
                [SVProgressHUD showErrorWithStatus:@"输入不能为空"];
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    if ([model.textString isEqualToString:@"学校"]) {
        STTools_TableViewDetailController * detail =
        [[STTools_TableViewDetailController alloc] initWithPlaceholder:@"请输入学校"
                                                           title:model.textString
                                                            text:model.detailString
                                                        complete:^(NSString *text, STTools_TableViewDetailController *detailControllerVC) {
            
            if (text.length && ![text isEqualToString:model.detailString]) {
                model.detailString = text;
                [self.tableView reloadData];
                [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"school" value:text handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                    
                }];
            }
            if (!text.length) {
                [SVProgressHUD showErrorWithStatus:@"输入不能为空"];
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    if ([model.textString isEqualToString:@"简介"]) {
        STTools_TableViewDetailController * detail =
        [[STTools_TableViewDetailController alloc] initWithPlaceholder:@"请输入简介"
                                                           title:model.textString
                                                            text:model.detailString
                                                        complete:^(NSString *text, STTools_TableViewDetailController *detailControllerVC) {
            
            if (text.length && ![text isEqualToString:model.detailString]) {
                model.detailString = text;
                [self.tableView reloadData];
                [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"brief" value:text handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                    
                }];
            }
            if (!text.length) {
                [SVProgressHUD showErrorWithStatus:@"输入不能为空"];
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [self.navigationController pushViewController:detail animated:YES];
    }
    if ([model.textString isEqualToString:@"性别"]) {
        NSArray * array = @[@"男",@"女",@"取消"];
        [self stoneobs_fun_st_showActionSheet:array andWithBlock:^(int tag) {
            if (tag < 2) {
                model.detailString = array[tag];
                [self.tableView reloadData];
                [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"sex" value:@(tag+1).description handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                    
                }];
            }
        }];
    }
    if ([model.textString isEqualToString:@"生日"]) {
        STTools_PickerViewController * picker = [[STTools_PickerViewController alloc] initWithDefualtDatePickerWithHandle:^(NSDate *date) {
            

            model.detailString = date.stoneobs_fun_yyyyMMddByLineWithDate;
            [self.tableView reloadData];
            [THUserDataController stoneobs_fun_send_change_user_info_request_key:@"birthday" value:model.detailString handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                
            }];
        }];
        [self presentViewController:picker animated:NO completion:nil];
        
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
